<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;height: 56px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-select placeholder="请选择类型" v-model="filters.group">
						<el-option v-for="item in groups" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="fnGetPower">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" @click="fnOpenPower">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		
		<!--权限列表-->
		<el-table :data="powers" border v-loading="powersLoading" class="powers-list">
			<el-table-column type="index" label="#" width="60">
			</el-table-column>
			<el-table-column prop="Group" label="类型" :formatter="fnGroupFormat">
			</el-table-column>
			<el-table-column prop="EmpID" label="员工账号">
			</el-table-column>
			<el-table-column prop="EmpName" label="员工姓名">
			</el-table-column>
			<el-table-column prop="CreatedBy" label="创建人">
			</el-table-column>
			<el-table-column label="操作" width="65">
				<template scope="scope">
				    <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
				      	<el-button type="danger" size="mini" icon="custom-delete"  @click="fnDelete(scope.row.ID)"></el-button>
				    </el-tooltip>
				</template>
			</el-table-column>
		</el-table>
		
		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="部门">
					<el-select v-model="addForm.dept" placeholder="请选择" style="width: 42%;" @change="fnChangeDept">
						<el-option v-for="item in depts" :key="item.ORGANIZATION_ID" :label="item.ORGANIZATION_NAME" :value="item.ORGANIZATION_ID"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="人员">
					<el-select v-model="addForm.account" placeholder="请选择" style="width: 42%;">
						<el-option v-for="item in staffs" :key="item.USER_ACCOUNT" :label="item.USER_NAME" :value="item.USER_ACCOUNT">
							<span style="float: left">{{ item.USER_NAME }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.USER_ACCOUNT }}</span>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="类型">
					<el-select placeholder="请选择类型" v-model="addForm.group" style="width: 232px;">
						<el-option v-for="item in groups" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="fnAddPower">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    import Svc from '../../service/index.js'
    import moment from 'moment'
	export default {
		data() {
			return {
				loginInfo : {},//登录信息
				filters : {
					group : 'admin'
				},
				groups : [{
					label : '管理员',
					value : 'admin'
				},{
					label : '审核办公室',
					value : 'office'
				},{
					label : '后勤部门',
					value : 'logistics'
				}],
				powers : [], //权限列表
				powersLoading : false,
				depts : [], //所有部门
				staffs : [], //当前部门下的员工
				//新增内容
				addForm : {
					dept : '',
					account :'',
					group : ''
				},
				addFormVisible : false
			}
		},
		methods: {
			//显示message提示框
            openMessage(msg,type){
    	        this.$message({
		          	showClose: true,
		          	message: msg,
		          	type: type
		        });
		    },
		    //格式化 -> 类型
		    fnGroupFormat(row, column){
		    	var that = this
                var val = row[column.property];
                for (var i=0;i<that.groups.length;i++) {
                	var item = that.groups[i]
                	if (item.value == val) {
                		val = item.label
                		break
                	}
                }
	          	return val; 
		    },
		    //获取所有权限
		    fnGetPower(){
		    	var that = this
	            Svc.fnGetData('/api/setting/employee/'+that.filters.group,{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.powers = res.body.Data
	            	} else {
	            		that.openMessage("权限列表获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //获取所有部门信息
		    fnGetDepts(){
		    	var that = this
	            Svc.fnGetData('/api/account/alldepts',{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.depts = res.body.Data
	            	} else {
	            		that.openMessage("部门信息获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //选择部门
		    fnChangeDept(val){
		    	var that = this
		    	that.fnGetStaffs()
		    },
		    //获取当前部门的员工
		    fnGetStaffs(){
		    	var that = this
	            Svc.fnGetData('/api/account/deptuser/'+that.addForm.dept,{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.staffs = res.body.Data
	            	} else {
	            		that.openMessage("当前部门的员工信息获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //打开新增页面
		    fnOpenPower(){
		    	var that = this
		    	that.addFormVisible = true
		    	//获取所有部门信息
		    	that.fnGetDepts()
		    },
	      	//新增
	      	fnAddPower(){
	      		var that = this
		    	var params = {
		    		CreatedBy : that.loginInfo.Name,
		    		EmpID : that.addForm.account,
					EmpName : '',
					Group : that.addForm.group,
		    	}
		    	//获取姓名
		    	for (var i=0;i<that.staffs.length;i++) {
		    		var item = that.staffs[i]
		    		if (item.USER_ACCOUNT == that.addForm.account) {
		    			params.EmpName = item.USER_NAME
		    			break
		    		}
		    	}
		    	
	            Svc.fnPostData('/api/setting/employee/save',params,function(res){
	            	if (res.body.ErrCode == 0){
	            		that.addFormVisible = false
	            		that.openMessage("新增操作成功！",'success')
	            		that.fnGetPower()
	            	} else {
	            		that.openMessage("新增操作失败！" + res.body.ErrMsg,'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //删除
		    fnDelete(id){
		    	var that = this
		    	that.$confirm('您确认删除本条记录吗？', '提示', {
					type: 'warning'
				}).then(function(){
					Svc.fnPostData('/api/setting/employee/remove',{ID:id},function(res){
		            	if (res.body.ErrCode == 0){
		            		that.openMessage("删除操作成功！",'success')
		            		that.fnGetPower()
		            	} else {
		            		that.openMessage("删除操作失败！",'error')
		            	}
		            },function(res){
		            	that.openMessage("网络错误，请重新！",'error')
		            })
				})
		    }
		},
		mounted() {
			//获取登录信息
			this.loginInfo = JSON.parse(sessionStorage.getItem('user'));
		    
		    //获取所有权限
		    this.fnGetPower()
		}
	}

</script>

<style scoped>
	.powers-list{
		width: 100%;
		margin-top: 20px;
	}

	.toolbar {
	    background: #f2f2f2;
	    padding: 10px;
	    margin: 10px 0;
	}
</style>